local sk__xianshou = fk.CreateSkill {

  name = "sk__xianshou",

  tags = {  },

}



sk__xianshou:addEffect(fk.TurnStart, {
  anim_type = "support",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(sk__xianshou.name)
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local to = room:askForChoosePlayers(player, table.map(room.alive_players, Util.IdMapper), 1, 1,
      "#sk__xianshou-choose", sk__xianshou.name, true)
    if #to > 0 then
      event:setCostData(self, to[1])
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = room:getPlayerById(event:getCostData(self))
    if to:hasSkill("sk__tiandao", true) then
      local mark = to:getTableMark("@sk__tiandao")
      if #mark == 0 then
        mark = {1, 1, 1, 1}
      end
      local judge = {
        who = to,
        reason = sk__xianshou.name,
        pattern = ".|.|^spade",
      }
      room:judge(judge)
      if to.dead then return end
      if judge.card.suit == Card.Spade then
        local choices = {}
        for i = 1, 4, 1 do
          if mark[i] > 0 then
            table.insert(choices, i)
          end
        end
        if #choices == 0 then return end
        local n = table.random(choices)
        mark[n] = mark[n] - 1
      else
        local n = math.random(1,4)
        mark[n] = mark[n] + 1
      end
      room:setPlayerMark(to, "@sk__tiandao", mark)
    else
      room:handleAddLoseSkills(to, "sk__tiandao", nil, true, false)
    end
  end,
})

return sk__xianshou